package com.zp.base.impl.HisDataIcd10;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.HisDataIcd10.HisDataIcd10;
import com.zp.base.entity.HisDataIcd10.bo.HisDataIcd10Bo;
import com.zp.base.entity.HisDataIcd10.vo.HisDataIcd10Vo;
import com.zp.base.mapper.HisDataIcd10.HisDataIcd10Mapper;
import com.zp.base.service.HisDataIcd10.IHisDataIcd10Service;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 数据，ICD-10编码Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class HisDataIcd10ServiceImpl implements IHisDataIcd10Service {

    private final HisDataIcd10Mapper baseMapper;

    /**
     * 查询数据，ICD-10编码
     */
    @Override
    public HisDataIcd10Vo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询数据，ICD-10编码列表(分页)
     */
    @Override
    public TableDataInfo<HisDataIcd10Vo> queryPageList (HisDataIcd10Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDataIcd10> lqw = buildQueryWrapper(bo);
        Page<HisDataIcd10Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据，ICD-10编码列表(不分页)
     */
    @Override
    public List<HisDataIcd10Vo> queryList (HisDataIcd10Bo bo) {
        LambdaQueryWrapper<HisDataIcd10> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisDataIcd10> buildQueryWrapper (HisDataIcd10Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisDataIcd10> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPid() != null, HisDataIcd10::getPid, bo.getPid());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), HisDataIcd10::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosisType()), HisDataIcd10::getDiagnosisType, bo.getDiagnosisType());
        lqw.eq(StringUtils.isNotBlank(bo.getZhongwenMingcheng()), HisDataIcd10::getZhongwenMingcheng, bo.getZhongwenMingcheng());
        lqw.eq(StringUtils.isNotBlank(bo.getPinyinIndex()), HisDataIcd10::getPinyinIndex, bo.getPinyinIndex());
        lqw.eq(StringUtils.isNotBlank(bo.getKeywordShuoming()), HisDataIcd10::getKeywordShuoming, bo.getKeywordShuoming());
        lqw.eq(StringUtils.isNotBlank(bo.getYingwenMingcheng()), HisDataIcd10::getYingwenMingcheng, bo.getYingwenMingcheng());
        lqw.eq(StringUtils.isNotBlank(bo.getShuoming()), HisDataIcd10::getShuoming, bo.getShuoming());
        lqw.eq(StringUtils.isNotBlank(bo.getOtherInfo()), HisDataIcd10::getOtherInfo, bo.getOtherInfo());
        lqw.eq(bo.getSubDirectNumber() != null, HisDataIcd10::getSubDirectNumber, bo.getSubDirectNumber());
        lqw.eq(bo.getSubTotalNumber() != null, HisDataIcd10::getSubTotalNumber, bo.getSubTotalNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getChineseWesternType()), HisDataIcd10::getChineseWesternType, bo.getChineseWesternType());
        return lqw;
    }

    /**
     * 新增数据，ICD-10编码
     */
    @Override
    public Boolean insertByBo (HisDataIcd10Bo bo) {
        HisDataIcd10 add = BeanUtil.toBean(bo, HisDataIcd10.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数据，ICD-10编码
     */
    @Override
    public Boolean updateByBo (HisDataIcd10Bo bo) {
        HisDataIcd10 update = BeanUtil.toBean(bo, HisDataIcd10.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (HisDataIcd10 entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据，ICD-10编码
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * icd10 字典自动补全检索
     *
     * @param hisDataIcd10Bo
     * @return
     */
    @Override
    public List<HisDataIcd10Vo> getAutoHisIcd (HisDataIcd10Bo hisDataIcd10Bo) {
        return baseMapper.getAutoHisIcd(hisDataIcd10Bo);
    }
}
